package huang.teamwork12;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserControl {
			//实现用户名注册的方法；
			public boolean registerU(User user){
				boolean result=false;
				BaseDao bd=new BaseDao();
				Connection con=bd.getCon();				
				String sql1="select username from users where username='"+user.getUserName()+"'";
				String sql2="insert into users (username,pwd) values(?,?)";
				
				try {
					PreparedStatement ps1=con.prepareStatement(sql1);
					ResultSet rs=ps1.executeQuery();
					while(rs.next())
					{
						System.out.println("用户名存在！！");
						if(rs!=null){
							rs.close();
						}
						if(ps1!=null){
							ps1.close();
						}
						return result;
				}
					PreparedStatement ps2=con.prepareStatement(sql2);
					ps2.setString(1, user.getUserName());
					ps2.setString(2, user.getPwd());
					result=ps2.execute();
					System.out.println("用户注册成功");
					if(ps2!=null){
						ps2.close();
					}
					if(rs!=null){
						rs.close();
					}
					if(ps1!=null){
						ps1.close();
					}
					if(con!=null){
						con.close();
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return result;
			}
			
			//实现登陆的方法：
			public boolean login(User user){
				boolean result =false;
				BaseDao bd=new BaseDao();
				Connection con=bd.getCon();
				String sql="select username,pwd from users where username='"+user.getUserName()+"'";
				try {
					PreparedStatement ps=con.prepareStatement(sql);
					ResultSet rs=ps.executeQuery();
					while(rs.next()){
						if(rs.getString("pwd").equals(user.getPwd())){
							System.out.println("登陆成功");
							result=true;
						}else{
							System.out.println("用户名或密码输入错误！！！");
						}}
						if(rs!=null){
							rs.close();
						}
						if(ps!=null){
							ps.close();
						}
						if(con!=null){
							con.close();
						}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return result;
			}
			//用户申请开店；
			public boolean openShop(User user){
				boolean result =false;	
				try {
					if(login(user))
					{	
						BaseDao bd=new BaseDao();
						Connection con=bd.getCon();				
						String sql1="select name from shops where name='"+user.getShop().getName()+"'";
						String sql2="insert into shops (name,username) values(?,?)";
						PreparedStatement ps1=con.prepareStatement(sql1);
						ResultSet rs=ps1.executeQuery();
						while(rs.next())
						{
							System.out.println("店名存在！！");
							if(rs!=null){
								rs.close();
							}
							if(ps1!=null){
								ps1.close();
							}
							return result;
						}	
						PreparedStatement ps2=con.prepareStatement(sql2);
						ps2.setString(1, user.getShop().getName());
						ps2.setString(2, user.getUserName());
						result=ps2.execute();
						System.out.println("店铺申请成功！！");
						
						if(rs!=null){
							rs.close();
						}
						if(ps1!=null){
							ps1.close();
						}
						if(ps2!=null){
							ps2.close();
						}
						if(con!=null){
							con.close();
						}
					}else{
						System.out.println("登陆失败！！！无法开店！！");
					
					}					
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return result;
			}		
}
